#include "widget.h"
#include "ui_widget.h"

#include <QClipboard>
#include <QApplication>
#include <QRegExp>

#include <QtDebug>

#include <QDateTime>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    qsrand(QDateTime::currentMSecsSinceEpoch());

    ui->retEdit->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_decBtn_clicked()
{
    QString old = ui->resEdit->text().trimmed();
    old.replace(QRegExp("[\u4e00-\u9fa5]"), "");
    ui->retEdit->setText(old);
}

void Widget::on_copyBtn_clicked()
{
    const QString res = ui->retEdit->text().trimmed();
    if(res.isEmpty()) return;
    auto *pClip = qApp->clipboard();
    pClip->setText(res);
}

void Widget::on_encBtn_clicked()
{
    QString url = ui->resEdit->text().trimmed();
    QStringList list;

    list << u8"测试"
         << u8"狗蛋"
         << u8"中文"
         << u8"儿豁"
         << u8"移除"
         << u8"调试"
         << u8"奥利给"
         << u8"牛批"
         << u8"这是"
         << u8"简单";

    const int listSize = list.size();

    for(int i=url.size() - 1; i>0; i-=2) {
        url.insert(i, list[qrand() % listSize]);
    }
    ui->retEdit->setText(url);
}

void Widget::on_clrBtn_clicked()
{
    ui->resEdit->clear();
    ui->retEdit->clear();
}

void Widget::on_closeBtn_clicked()
{
    this->close();
}
